#include <bits/stdc++.h>
using namespace std;
struct Pos{
int r, c;
int f;
Pos(int rr = 0, int cc = 0, int ff = 0) : r(rr), c(cc), f(ff){}
};
const int maxLofMaze = 8;
const int maxLofqueue = 100;
int maze[maxLofMaze + 1][maxLofMaze + 1];
Pos que[maxLofqueue + 1];
int head = 0, tail = 1;
Pos dir[4] = {Pos(-1, 0), Pos(1, 0), Pos(0, -1), Pos(0, 1)}; //用来移动方向
int main()
{
memset(maze,0xff,sizeof(maze));
int i, j;
for(i = 1; i <= 5; i++){
for(j = 1; j <= 5; j++){
scanf("%d", &maze[i][j]);
}
}
que[0] = Pos(1, 1, -1);
while(head != tail){
Pos ps = que[head];
if(ps.r == 5 && ps.c == 5){
vector<Pos> vect;
while(true){
vect.push_back(Pos(ps.r, ps.c, 0));
if(ps.f == -1) break;
ps = que[ps.f];
}
for(i = vect.size() - 1; i >= 0; i--){
printf("(%d, ", vect[i].r - 1);
printf("%d)\n", vect[i].c - 1);
}
return 0;
}else{
int r = ps.r, c = ps.c;
for(i = 0; i < 4; i++){
if(!maze[r + dir[i].r][c + dir[i].c]){
que[tail++] = Pos(r + dir[i].r, c + dir[i].c, head);
maze[r + dir[i].r][c + dir[i].c] = 1;
}
}
head++;
}
}
return 0;
}
百练 4127:迷宫问题
最新推荐文章于 2023-06-27 14:52:13 发布